package deprecated;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import measures.DistanceSet;
import measures.Fuzzy_Match_Score;

public class MapRed_Test {
	
	
	/**
	 * MR implementation of mapper and reducer
	 * @author wangler
	 *
	 */
	
	private static ArrayList<String> sentList = new ArrayList<String>();
	private static java.util.Map<String, Double> outpair = new HashMap<String, Double>();
	private static List<String> intermed = new ArrayList<String>();
	
	public static class Map {
		
		//variables
		static double fmTreshold = 0.7;
		static DistanceSet currSent;
		
		
		
		
		//map function
		public static void map(Long key, String value) throws IOException, InterruptedException{
			
			
			
			
			String line =  value.split("\\|\\|\\|")[0];	

			String target = value.split("\\|\\|\\|")[1];

			
			for(String sourceSent: sentList){
				
				String source = sourceSent.split("\\|\\|\\|")[1];
				String pos = sourceSent.split("\\|\\|\\|")[0];
				
				
				
				currSent = new DistanceSet(source, target);
			
				if(currSent.getMaxFMS() >= fmTreshold){	
					intermed.add(new String(pos) + "\t" + new String(line + "\t" + Fuzzy_Match_Score.computeFMS(currSent)));
				}
			
			}
		}
	}
	
	public static class Reduce{
		
		
		
		public void reduce(String key, Iterator<String> values) throws IOException, InterruptedException {
			
			//variables
			double maxScore = 0;
			String maxSentLine = null;
			
			while(values.hasNext() && maxScore<1){
				
				String curr_value = values.next();
				
				String[] val_split = curr_value.toString().split("\t");
				
				
				Double currVal = Double.parseDouble(val_split[1]);
				String currline = val_split[0].toString();

				if(currVal > maxScore){
					maxScore = currVal;
					maxSentLine = currline;
				}
			}	

			outpair.put(new String(key.toString() + "\t" + maxSentLine), new Double(maxScore));
		}
		
		
	}
	
	
	public static ArrayList<String> readFile(String file) throws IOException{
		
		FileInputStream stream;
		BufferedReader reader;
		ArrayList<String>testS = new ArrayList<String>();
		

			
			stream = new FileInputStream(file);
			reader = new BufferedReader(new InputStreamReader(stream));
			String line = reader.readLine();
			
			testS.ensureCapacity(1000);
			
			while(line != null){
				
				testS.add(line);
				line = reader.readLine();
			}
				
		
		
		
		reader.close();
		
		return testS;	
		
		
	}
	
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		
		List<String> input = new ArrayList<String>();
		input.add("13|||dies ist ein testsatz");
		input.add("3|||dies ist ein anderer satz");
		input.add("142|||hier steht noch was anderes");
		input.add("12|||auch der ist sehr anders");
		input.add("54|||das hier ist der letzte test");
		
		sentList.add("12|||ein satz ohne gleichen");
		sentList.add("11|||dies ist auch ein anderer satz");
		sentList.add("10|||auch der hier ist komisch");
		sentList.add("3|||der letzte satz");
		
		for(String i:input){
			
			Map.map(new Long(12), i);
		
		}
		System.out.println(intermed.toString());
		
	}

}
